<template>
	<view>
		<u-navbar title="收货地址"></u-navbar>
		
		<view class="addressList_box">
			<view class="addressList_con">
				<view class="addressItem" v-for="(item,index) in addressList" :key="index">
					<view class="consigneeCon">
						<view class="consigneeName">{{item.consignee}}</view>
						<view class="consigneeTel">{{item.mobile}}</view>
					</view>
					<view class="addressDetail">
						{{item.province}}{{item.city}}{{item.country}}{{item.address}}
					</view>
					<view class="addressPost">
						<view class="addressCheck" @click="setDefault(item.address_id)">
							<icon type="success" color="#2CAB68" class="totalSelect" v-if="item.is_default==1"/>
							<icon type="circle" class="totalSelect" v-else/>
							<text>设为默认地址</text>
						</view>
						<view class="addressDel" @click="delClick(item.address_id)">
							<u-icon name="trash" color="#999" size="40"></u-icon>
						</view>
					</view>
				</view>
			</view>
			<view class="dataEmpty_con">
				<u-empty v-if="addressList.length == 0"></u-empty>
			</view>
		</view>
		
		<!-- 底部操作 -->
		<view class="formPost_con">
			<view class="submitBtn" @click="addClick">+ 添加收货地址</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
			}
		},
		onLoad(option) {
			// uni.showLoading({
			// 	title: '加载中'
			// });
		},
		onShow() {
			var _this = this;
			_this.getAddressList();
		},
		onReady() {

		},
		methods: {
			// 获取地址列表
			async getAddressList() {
				var _this = this;
				_this.$u.get('/api/user.address/address_list', {

				}).then(res => {
					console.log("==获取地址列表==");
					console.log(res);
					uni.hideLoading();
					_this.addressList = res.data;
				}).catch(res => {
					console.log(res);
				})
			},
			
			// 设为默认地址
			setDefault(address_id) {
				var _this = this;
				_this.$u.post('/api/user.address/address_default', {
					id: address_id,
				}).then(res => {
					uni.hideLoading();
					console.log("==设为默认地址结果==");
					console.log(res);
			
					uni.showToast({
						title: res.msg,
						duration: 2000
					});
					_this.getAddressList();
				}).catch(res => {
					console.log(res);
				})
			},
			
			// 删除地址
			delClick(address_id) {
				var _this = this;
				_this.$u.post('/api/user.address/address_delete', {
					id: address_id,
				}).then(res => {
					uni.hideLoading();
					console.log("==删除地址结果==");
					console.log(res);
			
					uni.showToast({
						title: res.msg,
						duration: 2000
					});
					_this.getAddressList();
				}).catch(res => {
					console.log(res);
				})
			},
			
			// 新增
			addClick(){
				uni.navigateTo({
					url: '../addressForm/addressForm',
				})
			},
		}
	}
</script>

<style scoped>
	.addressList_box {
		margin-bottom: 150rpx;
		overflow: hidden;
	}

	.addressItem {
		padding: 30rpx 20rpx;
		margin-top: 20rpx;
		background-color: #fff;
		overflow: hidden;
	}

	.consigneeCon {
		font-size: 32rpx;
		color: #333;
		overflow: hidden;
		display: flex;
		align-items: center;
	}

	.consigneeTel {
		margin-left: 30rpx;
	}

	.addressDetail {
		font-size: 24rpx;
		color: #000;
		margin: 20rpx 0;
		overflow: hidden;
	}

	.addressPost {
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.addressCheck {
		display: flex;
		align-items: center;
	}

	.addressCheck text {
		margin-left: 10rpx;
	}
	
	/* 底部 */
	.formPost_con {
		width: 100%;
		padding: 0;
		position: fixed;
		bottom: 0;
	}
	
	.submitBtn {
		border-radius: 0;
	}
</style>
